package charu;

import java.util.Arrays;

public class ChaRu {
    public static void main(String[] args) {
        int n;
        int[] arr = new int[10];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) (Math.random() * 10 + 1);
        }
        System.out.println("Arrays.toString(arr) = " + Arrays.toString(arr));
        a(arr, arr.length);
        c();
    }

    //1.插入排序
    public static void a(int[] arr, int n) {
        int r;
        for (int i = 1; i < n; i++) {
            for (int j = i; j > 0; j--) {//j=5
                if (arr[j - 1] > arr[j])//arr[1] arr[2]
                {
                    r = arr[j - 1];
                    arr[j - 1] = arr[j];
                    arr[j] = r;
                }
            }
        }
        System.out.println("Arrays.toString(arr) = " + Arrays.toString(arr));
    }

    //    2.
    private static void c() {
        int[] arr = new int[10];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) (Math.random() * 10 + 1);
        }
        System.out.println("Arrays.toString(arr) = " + Arrays.toString(arr));
        System.out.println("开始进行插入排序法");
        long begin = System.currentTimeMillis();
        for (int i = 1; i < arr.length; i++) {
            for (int j = 0; j < i; j++) {
                if (arr[i] < arr[j]) {
                    int temp = arr[i]; // 33
                    System.arraycopy(arr, j, arr, j + 1, (i - j));
                    arr[j] = temp;
                }
            }
        }
        System.out.println("Arrays.toString(arr) = " + Arrays.toString(arr));
        long end = System.currentTimeMillis();
        System.out.println("结束插入排序法");
        long time = end - begin;
        System.out.println("选择插入法花费时间是：" + time + "毫秒");
    }

}
